package com.kefu.admin.modules.sys.controller;


import com.kefu.admin.modules.comm.controller.AbstractController;
import com.kefu.admin.modules.sys.model.SysDeptTreeModel;
import com.kefu.admin.modules.sys.service.SysDeptService;
import com.kefu.admin.utils.R;
import com.kefu.admin.modules.sys.model.SysDeptEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 部门管理
 *
 * @date 2017-06-20 15:23:47
 */
@Controller
@RequestMapping("/sys/dept")
public class SysDeptController extends AbstractController {
    @Autowired
    private SysDeptService sysDeptService;

    /**部门表入口*/
    @GetMapping("/list")
    public String list(){
        return "modules/sys/dept/list";
    }

    /**
     * 列表
     */
    @RequestMapping("/query")
    @ResponseBody
    public List<SysDeptTreeModel> query() {
        Map<String, Object> map = new HashMap<>();
        String deptId= getDeptId();
        map.put("deptFilter", sysDeptService.getSubDeptIdList(getDeptId()));
        List<SysDeptTreeModel> deptList = sysDeptService.queryList(map);
        return deptList;
    }


    @RequestMapping("/queryDeptById")
    @ResponseBody
    public SysDeptTreeModel queryDeptById(String deptId) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", getDeptId());
        if(null!=deptId && !"".equals(deptId)){
            map.put("id", deptId);
        }
        List<SysDeptTreeModel> deptList = sysDeptService.queryList(map);
        if(null!=deptList && deptList.size()>0){
            return deptList.get(0);
        }
        return null;
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("sys:dept:save")
    @ResponseBody
    public R save(@ModelAttribute("dept") SysDeptEntity dept) {
        sysDeptService.save(dept);

        return R.ok("添加成功！");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ResponseBody
    //@RequiresPermissions("sys:dept:update")
    public R update(@ModelAttribute("dept")  SysDeptEntity dept) {
        sysDeptService.update(dept);

        return R.ok("修改成功");
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("sys:dept:delete")
    @ResponseBody
    public R delete(String deptId) {
        //判断是否有子部门
        List<String> deptList = sysDeptService.queryDetpIdList(deptId);
        if (deptList.size() > 0) {
            return R.error("请先删除子部门");
        }
        sysDeptService.delete(deptId);

        return R.ok();
    }
}
